package com.jiandan.plugin

import com.android.build.gradle.api.ApkVariantOutput
import org.gradle.api.Plugin
import com.jiandan.plugin.task.FirmDownloadTask
import com.jiandan.plugin.task.FirmTask
import org.gradle.api.Project

class FirmPlugin implements Plugin<Project> {


    @Override
    void apply(Project project) {
        project.extensions.create("firmPlugin", PluginExtension)
        project.afterEvaluate {
            PluginExtension config = PluginExtension.getConfig(project)
            FirmDownloadTask firmDownloadTask = project.tasks.create("firmDownloadTask", FirmDownloadTask)
            FirmTask firmTask = project.tasks.create("firmTask", FirmTask)
            config.initConfig(project)
            firmTask.dependsOn(firmDownloadTask)
            if (config.needBuildApk()) {
                def assembleTask = project.tasks.getByPath("assembleRelease")
                firmTask.dependsOn(assembleTask)
                if (config.cleanBeforeBuildApk) {
                    //firmDownloadTask cleanTask  assembleTask firmTask
                    def cleanTask = project.tasks.findByPath("clean")
                    firmTask.dependsOn(cleanTask)
                    assembleTask.mustRunAfter(cleanTask)
                    cleanTask.mustRunAfter(firmDownloadTask)
                } else {
                    //firmDownloadTask assembleTask firmTask
                    assembleTask.mustRunAfter(firmDownloadTask)
                }
            }
            project.android.applicationVariants.all { variant ->

                if (!"release".equalsIgnoreCase(variant.buildType.name)) {
                    return
                }
                String name = variant.flavorName
                //多渠道打包
                String channelName = ""
                if (name != null && name.length() > 1) {
                    String first = name[0].toUpperCase()
                    channelName = name.replaceFirst(name[0], first)
                }
                File apk = variant.outputs
                        .find { variantOutput -> variantOutput instanceof ApkVariantOutput }
                        .outputFile
                project.tasks.getByPath("assemble${channelName}Release").doLast {
                    // 不是firmTask 并且不关联release
                    if (!config.getIsFirmTask() && !config.attachAssembleRelease) {
                        return
                    }
                    println("channel build success " + apk.absolutePath)
                    config.setApkIsBuild(true)
                    config.apkFilePath = apk.absolutePath
                    if (config.getIsFirmTask()) {
                        //会自动执行task
                        return
                    }
                    println("attachAssembleRelease true ")
                    firmTask.firmApkAndUpload()
                }
            }
//            def firTask = project.tasks.create(name: "fir-loader", description: "打包上传到fir.im", group: "firLoader") {
//
//            }
            //有颜色的输出
//            def ANSI_RESET = "\u001B[0m"
//            def ANSI_BLACK = "\u001B[30m"
//            def ANSI_RED = "\u001B[31m"
//            def ANSI_GREEN = "\u001B[32m"
//            def ANSI_YELLOW = "\u001B[33m"
//            def ANSI_BLUE = "\u001B[34m"
//            def ANSI_PURPLE = "\u001B[35m"
//            def ANSI_CYAN = "\u001B[36m"
//            def ANSI_WHITE = "\u001B[37m"
//            println "${ANSI_GREEN}GREEN COLORED${ANSI_RESET}NORMAL"
//            println "${ANSI_YELLOW}GREEN COLORED${ANSI_RESET}NORMAL"
//            println("firmTask.doFirst-------->" + config.printUpdateJson)

        }

    }

}
